<?php
// **************************************************************
// | Name: 星空框架  财务服务相关
// **************************************************************
// | Version: V5.0
// **************************************************************
// | Author： xkyinzhe <zhonghuaxinxing@sina.cn>
// **************************************************************
// | City: 青岛
// **************************************************************
// | Time: 2021-10-28 08:54
// **************************************************************
declare(strict_types=1);

namespace app\common\services;

use app\common\model\Finance as FinanceModel;
use app\common\model\User as UserModel;
use think\facade\Db;


/**
 * @xk 财务相关服务
 */
class FinanceServices
{

  public static function syncFinance($finance_data=[])
  {
      $financeModel = new FinanceModel();
      $userModel = new UserModel();
      $finance_data['finance_sn'] = $financeModel->getCodeNo(date('YmdHis'), 20, 1, 'finance_sn');
      $res = $financeModel->create($finance_data);
      // 开始 根据财务记录 写入用户消费统计
      if(in_array($finance_data['act'],[1,2]) && $finance_data['entry'] == 1){
        // 如果是用户支付 累计用户的消费总数
        $userModel
        ->where([['user_sn','=',$finance_data['relation_user_num']]])
        ->inc('purchases_order_price',(float)$finance_data['money'])
        ->update();
        $userModel
        ->where([['user_sn','=',$finance_data['relation_user_num']]])
        ->inc('purchases_num',1)
        ->update();
      }

      if($finance_data['entry'] == 2 && $finance_data['act'] == 4){
         // 用户退款 并处理成功 撤销用户消费总记录
         $userModel
         ->where([['user_sn','=',$finance_data['relation_user_num']]])
         ->dec('purchases_order_price',(float)$finance_data['money'])
         ->update();
         // 判断如果是整单售后则
      }


  }
}
